package com.elangzhi.ssm.modules.shop.services;

import com.elangzhi.ssm.model.Goods;
import com.elangzhi.ssm.modules.advice.services.AdviceService;
import com.elangzhi.ssm.modules.goods.services.GoodsService;
import com.elangzhi.ssm.modules.image.services.ImageService;
import com.elangzhi.ssm.modules.shop.dao.ShopDao;
import com.elangzhi.ssm.model.Shop;
import com.elangzhi.ssm.modules.shopEvent.services.ShopEventService;
import com.elangzhi.ssm.modules.user.services.UserService;
import com.elangzhi.ssm.services.BaseService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
* 店铺 Service
* @author GaoXiang
* @version 1.0
*/
@Service
public class ShopService extends BaseService<Shop> {

    @Resource
    private ShopDao shopDao;

    @Resource
    private UserService userService;

    @Resource
    private GoodsService goodsService;

    @Resource
    private ImageService imageService;

    @Resource
    private ShopEventService shopEventService;

    @Resource
    private AdviceService adviceService;

    /**
    * 保存数据
    * @param shop 实体对象
    * @return 实体id
    * @throws Exception 数据保存异常
    */
    public Integer insert(Shop shop) throws Exception {
        return shopDao.insert(shop);
    }

    /**
    * 根据 id 修改
    * @param shop 带id的实体对象
    * @return 受影响的行数
    * @throws Exception 数据修改异常
    */
    public Integer updateById(Shop shop) throws Exception {
        return shopDao.updateById(shop);
    }

    /**
    * 根据 id 删除
    * @param id 数据id
    * @return 受影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteById(Long id) throws Exception {
        return shopDao.deleteById(id);
    }

    /**
    * 根据 id 查找
    * @param id 实体id
    * @return 实体
    */
    public Shop selectById(Long id) {
        try {
            return shopDao.selectById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
    * 根据 id 批量删除
    * @param ids 要删除的id
    * @return 影响的行数
    * @throws Exception 数据删除异常
    */
    public Integer deleteByIds(List<Long> ids) throws Exception {
        return shopDao.deleteByIds(ids);
    }

    /**
    * 查询列表
    * @param map 参数
    * @return 列表
    * @throws Exception 数据返回异常
    */
    public PageInfo<Shop> list(Map map) throws Exception {
        return shopDao.list(map);
    }

    /**
    * 查询列表 带分页
    * @param map 参数
    * @param page 页码
    * @param size 每页大小
    * @return 列表
    * @throws Exception 数据返回异常
    */
    public PageInfo<Shop> list(Map map,int page,int size) throws Exception {
        return shopDao.list(map,page,size);
    }

    /**
     * 根据 id 查找店铺详情，带商品列表和bannerlist
     * @param id 实体id
     * @return 实体
     */
    public Shop findById(Long id) {
        Shop shop = null;
        try {
            shop = selectById(id);
            if(shop != null){
                //获取商品列表并给列表添加banner
                List<Goods> goodsList = goodsService.listByShopId(id);
                for(Goods goods : goodsList){
                    goods.setBannerList(imageService.listByTypeInfo(2,goods.getId()));
                }
                //给店铺添加商品列表
                shop.setGoodsList(goodsList);
                //给店铺添加banner
                shop.setBannerList(imageService.listByTypeInfo(1,id));
                //给店铺添加当前活动
                shop.setEventList(shopEventService.listByShopId(id));
                //添加店铺公告
                shop.setAdviceList(adviceService.listByTypeUser(3,id));
                //添加所属人user对象
                shop.setUser(userService.selectById(shop.getUserId()));
            }
            return shop;
        } catch (Exception e) {
            e.printStackTrace();
            return shop;
        }

    }

    /**
     * 获取用户店铺
     * @param userId 用户id
     * @return 店铺详情
     */
    public Shop findByUserId(Long userId) {

        try {
            Shop shop = shopDao.findByUserId(userId);
            return shop;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }
}
